﻿package  {
	import fl.events.SliderEvent;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import fl.controls.Slider;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.view.stats.StatsView;

	/**
	* ...
	* @author Default
	*/
	[ SWF( backgroundColor = '#cccccc', width = '800', height = '600', frameRate='60' ) ]
	public class MorphTest1 extends BasicView {
		
		private var body1:DAE;
		private var body2:DAE;
		private var bodyOnScene:DAE;
		
		//Flags
		private var isDragging:Boolean = false;
		private var stats:StatsView;
		
		private var morphSlider_breast:Slider;
		private var morphSlider_waist:Slider;
		private var morphSlider_pelvis:Slider;
		private var stateText:TextField;
	
		public function MorphTest1() {
			super(600, 600, false, false, "CAMERA3D");
			preparePv3d();
			loadAssets();
			createObjects();
			addControls();
			addListeners();
			startRendering();
		}
		
		public function preparePv3d():void {
			camera.z = -800;
			camera.zoom = 20;
			stats = new StatsView(renderer);
			stage.addChild(stats);
		}
		public function loadAssets():void {
			var daeFile:String = "assets/woman_body_deatached_mat2.DAE";
			body1 = new DAE();
			body1.load(daeFile);
			body1.addEventListener(FileLoadEvent.LOAD_PROGRESS, loadProgress);
			body1.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoadComplete);
			bodyOnScene = new DAE();
			bodyOnScene.load(daeFile);
			bodyOnScene.addEventListener(FileLoadEvent.LOAD_PROGRESS, loadProgress);
			bodyOnScene.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoadComplete);
			
			var daeFile2:String = "assets/woman_body_deatached_mat2_max.DAE";
			body2 = new DAE();
			body2.load(daeFile2);
			body2.addEventListener(ProgressEvent.PROGRESS, loadProgress);
			body2.addEventListener(Event.COMPLETE, daeLoadComplete);
		}

		public function createObjects():void {	
			bodyOnScene.scale = body1.scale = body2.scale = 30;
			bodyOnScene.y  = -150;
			bodyOnScene.rotationX = 90;
			bodyOnScene.rotationY = 30;
			scene.addChild(bodyOnScene);
		}
		public function addControls():void {
			morphSlider_breast = new Slider();
			stage.addChild(morphSlider_breast);
			morphSlider_breast.y = 100;
			morphSlider_breast.x = 610;
			morphSlider_breast.name = "Breast";
			
			morphSlider_waist = new Slider();
			stage.addChild(morphSlider_waist);
			morphSlider_waist.y = 200;
			morphSlider_waist.x = 610;
			morphSlider_waist.name = "Waist";
			
			morphSlider_pelvis = new Slider();
			stage.addChild(morphSlider_pelvis);
			morphSlider_pelvis.y = 300;
			morphSlider_pelvis.x = 610;
			morphSlider_pelvis.name = "Hips";
			
			morphSlider_breast.liveDragging = morphSlider_waist.liveDragging = morphSlider_pelvis.liveDragging = true;
			morphSlider_breast.maximum = morphSlider_waist.maximum = morphSlider_pelvis.maximum = 100;
			morphSlider_breast.minimum = morphSlider_waist.minimum = morphSlider_pelvis.minimum = 0;
			morphSlider_breast.width = morphSlider_waist.width = morphSlider_pelvis.width = 180;
			
			stateText = new TextField();
			stateText.autoSize = TextFieldAutoSize.LEFT;
			var format:TextFormat = new TextFormat("Arial", 10, 0xff00000)
			stateText.defaultTextFormat = format
			stage.addChild(stateText)
			stateText.x = 200;
			stateText.y = 5;
			stateText.text = "Loading Collada";
		}
		public function addListeners():void {
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			morphSlider_breast.addEventListener(SliderEvent.CHANGE, onSliderChangeHandler);
			morphSlider_waist.addEventListener(SliderEvent.CHANGE, onSliderChangeHandler);
			morphSlider_pelvis.addEventListener(SliderEvent.CHANGE, onSliderChangeHandler);
		}

		public function onPressHandler(event:MouseEvent):void {
			isDragging = true;
		}
		public function onMouseUpHandler(event:MouseEvent):void {
			isDragging = false;
		}
		public function onSliderChangeHandler(event:SliderEvent):void {
			trace(event.target.name)

			switch(event.target.name)
			{
				case("Breast"):
				{
					morphingThing("Breast-node-Node_001", morphSlider_breast.value/100);
					break;
				}
				case("Waist"):
				{
					morphingThing("Waist-node-Node_001", morphSlider_waist.value/100);
					break;
				}
				case("Hips"):
				{
					morphingThing("Pelvis-node-Node_001", morphSlider_pelvis.value/100);
					break;
				}
			}
			
		}
		public function morphingThing(childname:String, lerpstep:Number) {
			for (var i:int = 0; i < body1.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices.length; i++)
			{
				var v:Vertex3D = bodyOnScene.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i];
				v.x = lerp(body1.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].x, body2.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].x, lerpstep);
				v.y = lerp(body1.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].y, body2.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].y, lerpstep);
				v.z = lerp(body1.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].z, body2.getChildByName("COLLADA_Scene").getChildByName(childname).geometry.vertices[i].z, lerpstep);
				v.calculateNormal();
			}
			
			for each(var face:Triangle3D in body1.getChildByName("COLLADA_Scene").getChildByName(childname).faces)
				face.createNormal();
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			rotateCamera();
			super.onRenderTick(event);
		}
		private function rotateCamera():void{
			if(isDragging && mouseX < 600){
				var x:Number = (stage.mouseX - stage.stageWidth/2) / 4;
				camera.moveLeft(x);
				var y:Number = (stage.mouseY - stage.stageHeight/2) / 6;
				camera.moveDown(y);
				if (camera.y > 1000 || camera.y < -200) camera.moveDown(-y);
			}
		}
		private function loadProgress(e:FileLoadEvent):void {
			stateText.text = "Loading Collada: " + Math.floor((e.bytesLoaded / e.bytesTotal) * 100) + "%" + "  " +(e.bytesLoaded +"/"+ e.bytesTotal);
		}
		private function daeLoadComplete(e:Event):void {
			stateText.text = "Complete Collada";
		}
		public function lerp(coord1:Number, coord2:Number, lerpstep:Number):Number
		{
			return(coord1+(coord2-coord1)*lerpstep);
		}
	}	
}